package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public void saveWithFlavor(DishDto dishDto) {
      /*
        this.save(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();

       flavors =  flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;

        }).collect(Collectors.toList());

       dishFlavorService.saveBatch(flavors);*/
        //保存菜品基本信息
        this.save(dishDto);
        //将菜品的口味集合进行加工
        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历口味集合
       flavors =  flavors.stream().map((item)->{
           //将菜品的id与口味进行绑定
            item.setDishId(dishDto.getId());

            return item;
        }).collect(Collectors.toList());
        //添加菜品
       dishFlavorService.saveBatch(flavors);
    }
}
